package com.hotpot.common.base;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;
import com.hotpot.common.base.vm.ResultVM;
import com.hotpot.common.base.vm.SmartPageVM;
import com.hotpot.common.utils.param.ParamMap;
import com.hotpot.common.utils.param.StringTools;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.util.Map;

/**
 * 描述:
 * @author:ruikanwang
 * @date: 2018/1/3 0003
 * 注意：本内容仅限于新疆感知科技有限公司内部传阅,禁止外泄以及用于其他的商业目
 * Copyright © 2017-ganinfo. All rights reserved.
 */
public class BaseController<S extends IService<T>, T extends BaseModel<T>> {
	protected HttpServletRequest request;

	protected HttpServletResponse response;

	protected HttpSession session;

	@Autowired
	protected S service;

	public ResultVM baseGetSmartData(SmartPageVM<T> spage) {
		Page<T> page = new Page<T>(spage.getPagination().getStart()
				, spage.getPagination().getNumber());

		//设置默认排序条件
		if (StrUtil.isBlank(spage.getSort().getPredicate())) {
			spage.getSort().setPredicate("id");
		}
		page.setOrderByField(spage.getSort().getPredicate());
		page.setAsc(spage.getSort().getReverse());
		EntityWrapper<T> wrapper = new EntityWrapper<T>();
		if (spage.getSearch() != null) {
			Field[] fields = spage.getSearch().getClass().getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				try {
					fields[i].setAccessible(true);
					Object value = fields[i].get(spage.getSearch());
					if (null != value && !value.equals("")) {
						String fieldname = StringTools.underscoreName(fields[i].getName());
						wrapper.eq(fieldname, value.toString());
					}
					fields[i].setAccessible(false);
				} catch (Exception e) {
				}
			}
		}
		return  ResultVM.ok(service.selectPage(page,wrapper));
	}

	/**
	 * 新增
	 * @param t
	 * @return
	 */
	public ResultVM baseCreate(T t) {
		//t.setCreateUserId(ShiroUtils.getUserId());
//		t.setCreateTime(new Date());
//		t.setUpdateTime(new Date());
			//t.setUpdateUserId(ShiroUtils.getUserId());
			if(service.insert(t)){
			return ResultVM.ok();
		}else{
			return ResultVM.error();
		}
	}

	/**
	 * 更新
	 * @param t
	 * @return
	 */
	public ResultVM baseUpdate(T t) {
//		t.setUpdateTime(new Date());
		//t.setUpdateUserId(ShiroUtils.getUserId());
		if(service.updateById(t)){
			return ResultVM.ok();
		}else{
			return ResultVM.error();
		}
	}

	/**
	 * 根据id获取实体对象
	 * @param id
	 * @return
	 */
	public T baseGetInfo(String id) {
		return service.selectById(id);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public ResultVM baseDelete(String id) {
		if(service.deleteById(id)){
			return ResultVM.ok();
		}else{
			return ResultVM.error();
		}
	}

	/**
	 * 方法描述：得到PageData
	 * @return
	 * @return: ParamMap
	 */
	public ParamMap getTransmitMap() {
		return new ParamMap(this.getRequest());
	}

	/**
	 * 方法描述：得到request对象
	 * @return
	 * @return: HttpServletRequest
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}
}
